package com.ctlele.team.space.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.ctlele.team.space.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ImageDialog extends Dialog {

    private Button mOk;

    private Button mCancel;

    private String mOkText = "确定";

    private String mCancelText = "取消";

    private View.OnClickListener mCancelOnClick = (view) -> dismiss();

    private View.OnClickListener mOkOnClick = view -> {
    };

    private ImageView mImage;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    mImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    Bitmap bitmap = (Bitmap) msg.obj;
                    mImage.setImageBitmap(bitmap);
                    break;
            }
        }
    };

    public ImageDialog(@NonNull Context context) {
        super(context);
    }

    public void setOkText(String str) {
        mOkText = str;
    }

    public void setCancelText(String str) {
        mCancelText = str;
    }

    public void setOnCancel(View.OnClickListener listener) {
        mCancelOnClick = listener;
    }

    public void setOnOk(View.OnClickListener listener) {
        mOkOnClick = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_img_layout);
        mOk = findViewById(R.id.ok);
        mCancel = findViewById(R.id.cancel);
        mImage = findViewById(R.id.img_encrypted);
        this.setCanceledOnTouchOutside(false);
        initData();
    }

    private void initData() {
        mOk.setText(mOkText);
        mCancel.setText(mCancelText);
        mOk.setOnClickListener(mOkOnClick);
        mCancel.setOnClickListener(mCancelOnClick);
    }

    public void loadImage(final String imgUrl) {
        new Thread(() -> {
            final Message message = Message.obtain();
            HttpURLConnection connection = null;
            InputStream inputStream = null;
            try {
                URL url = new URL(imgUrl);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                int code = connection.getResponseCode();

                if (code == 200) {
                    inputStream = connection.getInputStream();
                    message.obj = BitmapFactory.decodeStream(inputStream);
                    message.what = 1;
                } else {
                    message.what = code;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
                message.what = -1;
            } catch (IOException e) {
                e.printStackTrace();
                message.what = -1;
            } finally {
                handler.sendMessage(message);

                if (connection != null) {
                    connection.disconnect();
                }
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    public void loadImage(byte[] bytes) {
        new Thread(() -> {
            final Message message = Message.obtain();
            try {
                message.obj = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                message.what = 1;
            } catch (Exception e) {
                e.printStackTrace();
                message.what = -1;
            } finally {
                handler.sendMessage(message);
            }
        }).start();
    }
}
